//
//  BSUserViewController.m
//  BS
//
//  Created by lanou on 15/8/24.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#define kURL1 @"http://api.budejie.com/api/api_open.php?a=profile&appname=baisishequ&asid=0F302804-E0E8-43DE-A3EE-9B538B4654AE&c=user&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&jbk=0&mac=&market=&openudid=5979ad73088ea5230ae9f104b993548b30416651&sex=m&udid=&uid=15567628&userid=15670832&ver=3.6.1"

#define kURL3 @"http://api.budejie.com/api/api_open.php?a=mytopic&appname=baisishequ&asid=0F302804-E0E8-43DE-A3EE-9B538B4654AE&c=topic&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&jbk=0&lastid=0&mac=&market=&openudid=5979ad73088ea5230ae9f104b993548b30416651&order_type=desc&sex=m&type=1&udid=&uid=15567628&ver=3.6.1&visit_uid=15670832"

#import "BSUserViewController.h"

#import "BSUserInfoModel.h"

#import "LJRequestManagerBlock.h"

#import "MBTwitterScroll.h"

#import "BSListModel.h"

#import "BSTextCell.h"

#import "BSHeaderView.h"

#import "BSFooterView.h"

#import "BSVideoCell.h"

#import "BSImageCell.h"

#import "PlayerManger.h"

#import "SDImageCache.h"

#import "MJRefresh.h"

#import "BSVoiceCell.h"

#import "BSWebCell.h"
@interface BSUserViewController ()<UITableViewDataSource, UITableViewDelegate, MBTwitterScrollDelegate>

@property (strong, nonatomic)NSMutableArray *topicsDataSource;

@property (strong, nonatomic)BSUserInfoModel *userInfo;

@property (strong, nonatomic)UITableView *tableView;
@end

@implementation BSUserViewController


- (instancetype)init{

    if (self = [super init]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (NSMutableArray *)topicsDataSource{

    if (_topicsDataSource == nil) {
        _topicsDataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _topicsDataSource;
}


- (void)regisetrCell{
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BSVideoCell" bundle:nil] forCellReuseIdentifier:@"videoCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BSTextCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BSImageCell" bundle:nil] forCellReuseIdentifier:@"imageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BSVoiceCell" bundle:nil] forCellReuseIdentifier:@"voiceCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BSWebCell" bundle:nil] forCellReuseIdentifier:@"webCell"];
    [self.tableView registerClass:[BSHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableView registerClass:[BSFooterView class] forHeaderFooterViewReuseIdentifier:@"footer"];
}

- (void)requestUserInfo:(NSString *)uid{

    [LJRequestManagerBlock manager:GET requestWith:kURL1 dic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dataDict = dict[@"data"];
        self.userInfo = [[BSUserInfoModel alloc] init];
        [self.userInfo setValuesForKeysWithDictionary:dataDict];
        MBTwitterScroll *myTableView = [[MBTwitterScroll alloc]
                                        initTableViewWithBackgound:self.userInfo.background_image
                                        avatarImage:self.userInfo.profile_image
                                        titleString:_userInfo.username
                                        subtitleString:_userInfo.introduction
                                        buttonTitle:@"关注"];  // Set nil for no button
        myTableView.tableView.delegate = self;
        myTableView.tableView.dataSource = self;
        myTableView.delegate = self;
        self.tableView = myTableView.tableView;
        [self.view addSubview:myTableView];
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        backBtn.frame = CGRectMake(20, 20, 40, 40);
        [self.view addSubview:backBtn];
        [self.view bringSubviewToFront:backBtn];
      
        [self regisetrCell];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
    
}


- (void)requestTopics:(NSDictionary *)dict{

    [LJRequestManagerBlock manager:GET requestWith:kURL3 dic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *topicsArray = dict[@"topics"];
        for (NSDictionary *dic in topicsArray) {
            BSListModel *model = [[BSListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.topicsDataSource addObject:model];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    [self requestUserInfo:nil];
    [self requestTopics:nil];
    
}


- (void)back{

    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [self.topicsDataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BSListModel *model = self.topicsDataSource[indexPath.section];
    if ([model.type isEqualToString:@"29"]) {
        BSTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        textCell.model = model;
        return textCell;
    }
    if ([model.type isEqualToString:@"10"]) {
        BSImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath];
        imageCell.vc = self;
        imageCell.model = model;
        return imageCell;
    }
    if ([model.type isEqualToString:@"41"]) {
        BSVideoCell *videoCell = [tableView dequeueReusableCellWithIdentifier:@"videoCell" forIndexPath:indexPath];
        videoCell.model = model;
        videoCell.vc = self;
        return videoCell;
    }
    if ([model.type isEqualToString:@"31"]) {
        BSVoiceCell *voiceCell = [tableView dequeueReusableCellWithIdentifier:@"voiceCell" forIndexPath:indexPath];
        voiceCell.model = model;
        voiceCell.vc = self;
        return voiceCell;
    }
    if ([model.type isEqualToString:@"51"]){
        BSWebCell *webCell = [tableView dequeueReusableCellWithIdentifier:@"webCell" forIndexPath:indexPath];
        webCell.model = model;
        webCell.nav = self.navigationController;
        return webCell;
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BSListModel *model = self.topicsDataSource[indexPath.section];
    if ([model.type isEqualToString:@"29"]) {
        return [BSTextCell heightOfCell:model];
    }
    if ([model.type isEqualToString:@"10"]) {
        return [BSImageCell heightOfCell:model];
    }
    if ([model.type isEqualToString:@"41"]) {
        return [BSVideoCell heightOfCell:model];
    }
    if ([model.type isEqualToString:@"31"]) {
        return [BSVoiceCell heightOfCell:model];
    }
    if ([model.type isEqualToString:@"51"]) {
        return 220;
    }
    return 0;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    BSHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    view.model = self.topicsDataSource[section];
//    view.nav = self.navigationController;
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    BSFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer"];
    view.model = self.topicsDataSource[section];
    view.nav = self.navigationController;
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 50;
}


#pragma mark - 视频播放划出屏幕停止
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    PlayerManger *manager = [PlayerManger defaultManger];
    NSArray *arrayIndex = [self.tableView indexPathsForVisibleRows];
    if(manager.mediaType == Vedio){
        if (![arrayIndex containsObject:manager.indexPath]) {
            [manager.view removeFromSuperview];
            [manager stop];
        }
    }
}

-(void)recievedMBTwitterScrollEvent{

    NSLog(@"下拉");
}


-(void)recievedMBTwitterScrollButtonClicked{

    NSLog(@"关注");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
